/*
 * 【BCE百度开放云产品：对象存储BOS】
 */
var config = require('config');
var request = require('request');

var BosClient = require('bce-sdk-js').BosClient;
var bosClient = new BosClient({
    endpoint: config.get('bos.endpoint'),
    credentials: config.get('credentials')
});

var putObject = function(url, object, callback) {
    var bucket = config.get('bos.bucket');
    // BosClient.generatePresignedUrl有改动，增加了method参数, 所以把BOS整个SDK提交了
    var signedUrl = bosClient.generatePresignedUrl(bucket, object, 'PUT');
    var bosUrl = bosClient.generateUrl(bucket, object);
    request.get(url).on('error', function(err) {
        console.log(err);
        callback(err, bosUrl);
    }).pipe(request.put(signedUrl).on('error', function(err){
        console.log(err);
        callback(err, bosUrl);
    }));

    callback(null, bosUrl);
};

exports.putObject = putObject;